#include<graphics.h>
#include <conio.h>
#include<math.h>

#pragma  comment (lib,"Winmm.lib")

//游戏画面尺寸
#define High 800
#define Width 500

IMAGE img_bk;		//背景图片

IMAGE img_planeNormal1, img_planeNormal2;	//正常飞机图片
int position_x, position_y;					//飞机坐标

IMAGE img_bullet1, img_bullet2;				//子弹图片
int bullet_x, bullet_y;						//子弹坐标

IMAGE img_enemyPlane1, img_enemyPlane2;		//敌机图片
int enemy_x, enemy_y;						//敌机位置

IMAGE img_planeExplode1, img_planeExplode2;	//敌机图片
int isExplode = 0;							//飞机是否爆炸
int score;									//得分

void startup()
{
	initgraph(Width, High);		//画布大小
	loadimage(&img_bk, _T("E:\\background.jpg"));	//读取图片到img对象中

	loadimage(&img_planeNormal1, _T("E:\\planeNormal_1.jpg"));		//遮罩图
	loadimage(&img_planeNormal2, _T("E:\\planeNormal_2.jpg"));

	loadimage(&img_bullet1, _T("E:\\bullet1.jpg"));		//子弹
	loadimage(&img_bullet2, _T("E:\\bullet2.jpg"));

	loadimage(&img_enemyPlane1, _T("E:\\enemyPlane1.jpg"));		//敌机
	loadimage(&img_enemyPlane2, _T("E:\\enemyPlane2.jpg"));

	loadimage(&img_planeExplode1, _T("E:\\planeExplode_1.gif"));
	loadimage(&img_planeExplode2, _T("E:\\planeExplode_2.gif"));

	//播放背景音乐
	mciSendString(TEXT("open E:\\game_music.mp3 alias bkmusic"), NULL, 0, NULL);
	mciSendString(TEXT("play bkmusic repeat "), NULL, 0, NULL);		//循环播放


	position_x = Width / 2;
	position_y = High*0.7;

	bullet_x = position_x;
	bullet_y = -100;

	enemy_x = Width / 2;
	enemy_y = 10;

	BeginBatchDraw();

}

void show()
{
	putimage(0, 0, &img_bk);		//显示背景

	if (isExplode == 0)
	{
		putimage(position_x - 50, position_y - 60, &img_planeNormal1, NOTSRCERASE);		//显示正常飞机
		putimage(position_x - 50, position_y - 60, &img_planeNormal2, SRCINVERT);

		putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);		//显示子弹
		putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);

		putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);		//显示敌机
		putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);
	}
	else
	{
		putimage(position_x - 50, position_y - 60, &img_planeExplode1, NOTSRCERASE);		//显示爆炸的飞机
		putimage(position_x - 50, position_y - 60, &img_planeExplode2, SRCINVERT);

	}

	FlushBatchDraw();

	Sleep(2);
}

void updateWithoutinput()
{
	if (isExplode == 0)
	{
		if (bullet_y>-50)
			bullet_y = bullet_y - 5;		//子弹向上移动

		if (enemy_y<High - 25)
			enemy_y = enemy_y + 1;			//敌机向下移动
		else
			enemy_y = 10;

		if (abs(enemy_x - bullet_x) + abs(enemy_y - bullet_y)<50)		//判断敌机被击中
		{
			enemy_x = rand() % Width;
			enemy_y = 0;
			mciSendString(TEXT("close 5music"), NULL, 0, NULL);			//先把前面一次的音乐关掉
			mciSendString(TEXT("open E:\\5.mp3 alias 5music"), NULL, 0, NULL);	//打开音乐
			mciSendString(TEXT("play 5music"), NULL, 0, NULL);	//仅播放一次

		}
		if (score % 10 == 0)
		{
			mciSendString(TEXT("close 10music"), NULL, 0, NULL);			//先把前面一次的音乐关掉
			mciSendString(TEXT("open E:\\10.mp3 alias 10music"), NULL, 0, NULL);	//打开音乐
			mciSendString(TEXT("play 10music"), NULL, 0, NULL);	//仅播放一次

		}
		if (abs(enemy_x - position_x) + abs(enemy_y - position_y)<150)		//判断飞机与敌机碰撞
		{
			isExplode = 1;
			mciSendString(TEXT("close exmusic"), NULL, 0, NULL);			//先把前面一次的音乐关掉
			mciSendString(TEXT("open E:\\explode.mp3 alias exmusic"), NULL, 0, NULL);	//打开音乐
			mciSendString(TEXT("play exmusic"), NULL, 0, NULL);	//仅播放一次

		}
	}
}

void updateWithinput()
{
	if (isExplode == 0)
	{
		//鼠标操作飞机
		MOUSEMSG m;			//记录鼠标消息
		while (MouseHit())
		{
			m = GetMouseMsg();

			if (m.uMsg == WM_MOUSEMOVE)	//鼠标移动时，飞机坐标等于鼠标位置
			{
				position_x = m.x;
				position_y = m.y;
			}
			else if (m.uMsg = WM_LBUTTONDOWN)	//按下鼠标左键时发射子弹
			{
				bullet_x = position_x;
				bullet_y = position_y - 80;
				mciSendString(TEXT("close fgmusic"), NULL, 0, NULL);			//先把前面一次的音乐关掉
				mciSendString(TEXT("open E:\\f_gun.mp3 alias fgmusic"), NULL, 0, NULL);	//打开音乐
				mciSendString(TEXT("play fgmusic"), NULL, 0, NULL);	//仅播放一次
			}
		}
	}
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}

int main()
{
	startup();		//数据初始化
	while (1)		//游戏循环执行
	{
		show();		//显示画面
		updateWithoutinput();		//与用户输入无关的更新
		updateWithinput();			//与用户输入有关的更新
	}
	return 0;
}